<?php

require_once eCMS_DIR . '/app/lib/FileManager/FileManager.class.php';
require_once eCMS_DIR . '/app/lib/FileManager/FileSystemLibrary.class.php';

require_once eCMS_DIR . '/module/user/PhotoLibrary/controller/PhotoLibraryLibrary.class.php';

class FileBrowser extends Page {

	protected function addPageMethods() {
		
		parent::addPageMethods();	
		
		if($this->canItemRead()) {
			$this->allowMethod("browse", "admin");
		}
	}
	
	public function index() {
		
		$fm = $this->_initFM();

		// Handle Requests, or Display a iFrane with the Browse Version of the FileManager
		if($fm->handleRequest()) { die(); };
		$this->pageTitle = "";
		
	}
	
	public function browse() {

		$fm = $this->_initFM();
		$fm->setDownloadMode();

		$this->addViewData("FileManager", $fm->writeFileManager(true));
		
		$this->setDesignMode("filemanager");
		
	}
	
	/**
	 * Prepares a FileManager Instance
	 * @return FileManager
	 */
	private function _initFM() {
		
		$cfg = Runtime::getConfiguration();
		
		$fm = new FileManager();
		$fm->setConnectorUrl($this->getUrl());
		
		$fm->setIconsUrl($cfg['sharedDesignURL']."/general/icons/files/");
		$fm->setIconsLocalPath(eCMS_DIR ."/content/design/general/icons/files/");
		
		$fm->addLibrary(new FileSystemLibrary("Public", $cfg['paths']['page'] . "/content/public"));
		/* Add Libreries for PhotoLibreries */
		$moduleList = new ModuleList();
		$moduleList->getByName("PhotoLibrary");
		
		foreach ($moduleList->At(0)->instances as $instance) {
			$fm->addLibrary(new PhotoLibraryLibrary($instance->pkModuleInstance));
		}
	
						
		return $fm;
	}
	
}

?>